﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Main.master" AutoEventWireup="true" CodeBehind="BatchEditing.aspx.cs" Inherits="StockCrawler.Temp.BatchEditing" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<%--<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="BatchEditing.aspx.cs" Inherits="GridEditing_BatchEditing" %>
<asp:Content runat="server" ContentPlaceHolderID="CustomHeadHolder">--%>
    <script type="text/javascript">
        function Grid_BatchEditStartEditing(s, e) {
            var productNameColumn = s.GetColumnByField("ProductName");
            if(!e.rowValues.hasOwnProperty(productNameColumn.index))
                return;
            var cellInfo = e.rowValues[productNameColumn.index];
            productNameTextBox.SetValue(cellInfo.value);
            if(e.focusedColumn === productNameColumn)
                productNameTextBox.SetFocus();
        }
        function Grid_BatchEditEndEditing(s, e) {
            var productNameColumn = s.GetColumnByField("ProductName");
            if(!e.rowValues.hasOwnProperty(productNameColumn.index))
                return;
            var cellInfo = e.rowValues[productNameColumn.index];
            cellInfo.value = productNameTextBox.GetValue();
            cellInfo.text = encodeHtml(productNameTextBox.GetText());
            productNameTextBox.SetValue(null);
        }
        function Grid_BatchEditRowValidating(s, e) {
            var productNameColumn = s.GetColumnByField("ProductName");
            var cellValidationInfo = e.validationInfo[productNameColumn.index];
            if(!cellValidationInfo) return;
            var value = cellValidationInfo.value;
            if(!ASPxClientUtils.IsExists(value) || ASPxClientUtils.Trim(value) === "") {
                cellValidationInfo.isValid = false;
                cellValidationInfo.errorText = "Product Name is required";
            }
        }
        var preventEndEditOnLostFocus = false;
        function ProductNameTextBox_KeyDown(s, e) {
            var keyCode = ASPxClientUtils.GetKeyCode(e.htmlEvent);
            if(keyCode !== ASPx.Key.Tab) return;
            var moveActionName = e.htmlEvent.shiftKey ? "MoveFocusBackward" : "MoveFocusForward";
            if(grid.batchEditApi[moveActionName]()) {
                ASPxClientUtils.PreventEventAndBubble(e.htmlEvent);
                preventEndEditOnLostFocus = true;
            }
        }
        function ProductNameTextBox_LostFocus(s, e) {
            if(!preventEndEditOnLostFocus)
                grid.batchEditApi.EndEdit();
            preventEndEditOnLostFocus = false;
        }
        function encodeHtml(text) {
            if(text === undefined || text === null)
                return "";
            var replacements = [
                [ /&amp;/g, '&ampx;' ], [ /&/g, '&amp;' ], [ /&quot;/g, '&quotx;' ], [ /"/g, '&quot;' ],
                [ /&lt;/g, '&ltx;' ], [ /</g, '&lt;' ], [ /&gt;/g, '&gtx;' ], [ />/g, '&gt;' ]
            ];
            for(var i = 0; i < replacements.length; i++) {
                var repl = replacements[i];
                text = text.replace(repl[0], repl[1]);
            }
            return text;
        }
    </script>
    <table class="OptionsTable BottomMargin">
    <tr><td>
        <dx:ASPxLabel ID="EditModeLabel" runat="server" Text="Edit mode:" AssociatedControlID="EditModeCombo" />
    </td><td>
        <dx:ASPxComboBox runat="server" ID="EditModeCombo" AutoPostBack="true" SelectedIndex="0">
            <Items>
                <dx:ListEditItem Value="Cell" />
                <dx:ListEditItem Value="Row" />
            </Items>
        </dx:ASPxComboBox>
    </td></tr><tr><td>
        <dx:ASPxLabel ID="StartEditActionLabel" runat="server" Text="Start edit action:" AssociatedControlID="StartEditActionCombo" />
    </td><td>
        <dx:ASPxComboBox runat="server" ID="StartEditActionCombo" AutoPostBack="true" SelectedIndex="0">
            <Items>
                <dx:ListEditItem Value="Click" />
                <dx:ListEditItem Value="DblClick" />
            </Items>
        </dx:ASPxComboBox>
    </td></tr>
    </table>
    <br />
    <dx:ASPxGridView ID="Grid" runat="server" DataSourceID="DemoDataSource1" KeyFieldName="ProductID" EnableRowsCache="false" ClientInstanceName="grid" Width="100%">
        <Columns>
            <dx:GridViewCommandColumn ShowNewButtonInHeader="true" ShowDeleteButton="True" />
            <dx:GridViewDataTextColumn FieldName="ProductName">
                <EditItemTemplate>
                    <dx:ASPxTextBox ID="ProductNameTextBox" runat="server" ClientInstanceName="productNameTextBox" Width="100%">
                        <ClientSideEvents KeyDown="ProductNameTextBox_KeyDown" LostFocus="ProductNameTextBox_LostFocus" />
                    </dx:ASPxTextBox>
                </EditItemTemplate>
            </dx:GridViewDataTextColumn>
            <dx:GridViewDataComboBoxColumn FieldName="CategoryID" Caption="Category Name">
                <PropertiesComboBox DataSourceID="CategoriesDataSource" ValueType="System.Int32" ValueField="CategoryID" TextField="CategoryName" />
            </dx:GridViewDataComboBoxColumn>
            <dx:GridViewDataTextColumn FieldName="QuantityPerUnit" />
            <dx:GridViewDataSpinEditColumn FieldName="UnitPrice">
                <PropertiesSpinEdit DisplayFormatString="c" DisplayFormatInEditMode="true" MinValue="0" MaxValue="60000">
                    <ValidationSettings Display="Dynamic" RequiredField-IsRequired="true" />
                </PropertiesSpinEdit>
            </dx:GridViewDataSpinEditColumn>
            <dx:GridViewDataSpinEditColumn FieldName="UnitsInStock">
                <PropertiesSpinEdit MinValue="0" MaxValue="10000" />
            </dx:GridViewDataSpinEditColumn>
            <dx:GridViewDataCheckColumn FieldName="Discontinued">
                <PropertiesCheckEdit AllowGrayed="true" AllowGrayedByClick="false" />
            </dx:GridViewDataCheckColumn>
        </Columns>
        <SettingsEditing Mode="Batch" />
        <ClientSideEvents BatchEditStartEditing="Grid_BatchEditStartEditing" BatchEditEndEditing="Grid_BatchEditEndEditing" 
            BatchEditRowValidating="Grid_BatchEditRowValidating" />
    </dx:ASPxGridView>
    <demo:demodatasource runat="server" ID="DemoDataSource1" IdentityKey="ProductID" DataSourceID="ProductsDataSource" IsSiteMode="True"></demo:demodatasource>
    <ef:entitydatasource runat="server" ID="ProductsDataSource"
        ContextTypeName="NorthwindContext"
        EnableDelete="True" EnableInsert="True" EnableUpdate="True"
        EntitySetName="Products">
    </ef:entitydatasource>
    <ef:entitydatasource runat="server" ID="CategoriesDataSource" ContextTypeName="NorthwindContext" EntitySetName="Categories"></ef:entitydatasource>
</asp:Content>
